package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

//详情页
public class DetailPage extends Utils {
    public static String url=detailUrl;


    public DetailPage()
    {
        super(url);
    }

    /**
     1.校验详情页是否正常加载
     */
    public void CheckPageRight() throws InterruptedException {
        Thread.sleep(1000);
        //标题
        String title=driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
        //发布时间
        String pushTime=driver.findElement(By.cssSelector("body > div.container > div.right > div > div.date")).getText();
          //内容---用父标签，内容删除，子标签会动态变化
        String content=driver.findElement(By.cssSelector("#detail")).getText();
          //编辑按钮
        String edit=driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.edit-btn")).getText();

        //删除按钮
        String del=driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.dele-btn")).getText();

        //校验文本
        assert !title.isEmpty();//不能为空
        assert !pushTime.isEmpty();
        assert !content.isEmpty();
        assert edit.equals("编辑");//文本保持一致
        assert del.equals("删除");
    }
/**
 2.编辑按钮----跳转到博客更新页
 */
public void EditBtnDetail() throws InterruptedException {
    //获取详情页博客内容
    String title=driver.findElement(By.cssSelector("#detail")).getText();

    //点击编辑按钮
    driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.edit-btn")).click();

    Thread.sleep(1000);
    //获取更新页博客内容
    String updateTitle=driver.findElement(By.cssSelector("#editor > div.editormd-preview > div")).getText();

//    System.out.println(title);
//    System.out.println(updateTitle);

    //校验
    //跳转前后博客内容相同
    assert updateTitle.equals(title);

    //返回上一级
    driver.navigate().back();

}

/**
 3.删除按钮----跳转到博客更新页
 */
public  void delBtnDetail() throws InterruptedException {
    //点击删除按钮
    driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.dele-btn")).click();
    //等待弹窗出现
    wait.until(ExpectedConditions.alertIsPresent());
   // Thread.sleep(2000);
    //处理错误弹窗

    //选择“取消”---返回当前页
    Alert cancelAlert = driver.switchTo().alert();
    cancelAlert.dismiss(); // 点击取消

    // 验证页面不变
    assert driver.getCurrentUrl().equals(url);


    //点击删除按钮
    driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button.dele-btn")).click();
    //等待第一个弹窗出现
    wait.until(ExpectedConditions.alertIsPresent());
    //选择”确定”
    Alert alert = driver.switchTo().alert();
    alert.accept();

    //等待第二个弹窗出现
    wait.until(ExpectedConditions.alertIsPresent());
    //选择”确定”----跳转到列表页
    alert.accept();


}
}
